package Demo1;

public class MyQueue {
    static class Node {
        public int val;
        public Node next;
        public Node prev;

        public Node(int val){
            this.val = val;
        }
    }

    public Node front;
    public Node rear;

    public void offer(int val) {
        Node node = new Node(val);

        if(front == null) {
            front = node;
            rear = node;
        }else {
            rear.next = node;
            node.prev = rear;
            rear = node;
        }
    }

    public int poll() {
        if(front == null) {
            throw new IndexOutOfBoundsException("");
        }

        int val = front.val;
        front = front.next;

        if(front != null) {
            front.prev = null;
        }

        return val;
    }

    public int peek() {
        if(front == null) {
            throw new IndexOutOfBoundsException("");
        }

        return front.val;
    }

}
